/*
 * Created on 08.10.2006 by leck
 *
 */
package de.devisnik.eidle;

import junit.framework.TestCase;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import de.devisnik.eidle.IScreenHandler;
import de.devisnik.eidle.ScreenHandler;
import de.devisnik.eidle.screen.IScreen;
import de.devisnik.eidle.screen.IScreenListener;

public class ScreenHandlerTest extends TestCase {

    private final class DoneFiringScreen extends NullScreen {

        private IScreenListener itsListener;

//        @Override
        public void addScreenListener(IScreenListener screenListener) {
            itsListener = screenListener;
        }
        
//        @Override
        public void removeScreenListener(IScreenListener screenListener) {
            itsListener = null;
        }

//        @Override
        public void createControl(Composite parentShell) {
            if (itsListener != null) {                
                itsListener.done(this);
            }
        }
        
        @Override
        public void setFocus() {
            if (itsListener != null) {                
                itsListener.done(this);
            }
        }
    }

    private final class TestScreen extends NullScreen {

        private final boolean[] itsCreated;

        private TestScreen(boolean[] created) {
            itsCreated = created;
        }

//        @Override
        public void createControl(Composite parentShell) {
            itsCreated[0] = true;
        }

//        @Override
        public void disposeControl() {
            itsCreated[0] = false;
        }
    }

    public void testActivate() {
        Display display = new Display();
        try {
            IScreenHandler handler = new ScreenHandler(new Shell(display));

            final boolean[] isControlCreated = { false };
            handler.activate(new TestScreen(isControlCreated));
            assertEquals(true, isControlCreated[0]);

            final boolean[] isAnotherControlCreated = { false };
            handler.activate(new TestScreen(isAnotherControlCreated));
            assertEquals(true, isAnotherControlCreated[0]);
            assertEquals(false, isControlCreated[0]);
        }
        finally {
            display.dispose();
        }
    }

    public void testDispose() {
        Display display = new Display();
        try {
            IScreenHandler handler = new ScreenHandler(new Shell(display));

            final boolean[] isControlDisposed = { false };
            handler.activate(new NullScreen() {
//                @Override
                public void disposeControl() {
                    isControlDisposed[0] = true;
                }
            });
            handler.dispose();
            assertEquals(true, isControlDisposed[0]);
        }
        finally {
            display.dispose();
        }
    }
    
    public void testGetCurrent() {
        Display display = new Display();
        try {
            IScreenHandler handler = new ScreenHandler(new Shell(display));
            NullScreen screen = new NullScreen();
            handler.activate(screen);
            assertSame(screen, handler.getCurrent());
        }
        finally {
            display.dispose();
        }        
    }
    
    public void testAddListeners() {
        Display display = new Display();
        try {
            IScreenHandler handler = new ScreenHandler(new Shell(display));
            final int[] doneCalled = {0};
            handler.addScreenListener(new IScreenListener() {

                public void done(IScreen screen) {
                    doneCalled[0]++;
                }
            });
            IScreen screen = new DoneFiringScreen();
            handler.activate(screen);
            assertEquals(1, doneCalled[0]);
            
            doneCalled[0] = 0;
            handler.activate(new DoneFiringScreen());
            assertEquals(1, doneCalled[0]);

            // test if listener is removed from old screen
            doneCalled[0] = 0;
            screen.createControl(null);
            assertEquals(0, doneCalled[0]);
            
        }
        finally {
            display.dispose();
        }       
    }

    public void testAddListenersAfterActivate() {
        Display display = new Display();
        try {
            IScreenHandler handler = new ScreenHandler(new Shell(display));
            final int[] doneCalled = {0};
            IScreen screen = new DoneFiringScreen();
            handler.activate(screen);
            handler.addScreenListener(new IScreenListener() {
                
                public void done(IScreen screen) {
                    doneCalled[0]++;
                }
            });
            screen.createControl(null);
            assertEquals(1, doneCalled[0]);
        }
        finally {
            display.dispose();
        }       
    }
    
    
}
